//
//  PersonInfoHeaderView.swift
//  maosheng
//
//  Created by lxl on 2019/8/15.
//

import UIKit

class PersonInfoHeaderView: UIView {
    let userImg = UIImageView()
    let userNameLab = UILabel()
    let mainView = UIView()
    let descBtn = UIButton(type: .custom)
    let bottomLab = UILabel()
    let editBtn = UIButton(type: .custom)//跳转到编辑主页的

    override init(frame: CGRect) {
        super.init(frame: frame)
        setHeaderView()
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    func setHeaderView()  {
        let bgImg = UIImageView()
        bgImg.image = UIImage(named: "person_bg")
        self.addSubview(bgImg)
        bgImg.snp.makeConstraints { (make) in
            make.left.top.right.equalTo(0)
            make.height.equalTo(170 * rateOfWidth)
        }
        self.addSubview(mainView)
        mainView.backgroundColor = .white
        mainView.snp.makeConstraints { (make) in
            make.top.equalTo(bgImg.snp.bottom)
            make.left.right.equalTo(0)
            make.height.equalTo(222 )
        }
        
        
        let imgHeight = 100
        userImg.layer.cornerRadius = CGFloat(imgHeight/2)
        userImg.layer.masksToBounds = true
        userImg.contentMode = .scaleAspectFit
        userImg.image = UIImage(named: "login_head_moren")
        self.addSubview(userImg)
        userImg.snp.makeConstraints { (make) in
            make.left.equalTo(15)
            make.width.height.equalTo(imgHeight)
            make.centerY.equalTo(mainView.snp.top)
        }
        
        mainView.addSubview(userNameLab)
        userNameLab.text =  "未设置昵称"
        userNameLab.textAlignment = .left
        userNameLab.textColor = KYP_333333_COLOR
        userNameLab.font = UIFont(name: PF_Medium, size: 22)
        userNameLab.snp.makeConstraints { (make) in
            make.left.equalTo(15)
            make.top.equalTo(userImg.snp.bottom).offset(17)
        }
        
        
        descBtn.contentHorizontalAlignment = .left
        descBtn.titleLabel?.numberOfLines = 0
        descBtn.setTitleColor(KYP_999999_COLOR, for: .normal)
        descBtn.setTitle(" 这家伙很懒，什么也没留下", for: .normal)
        descBtn.setImage(UIImage(named: "person_edit"), for: .normal)
        descBtn.titleLabel?.font = UIFont.systemFont(ofSize: 13)
        mainView.addSubview(descBtn)
        descBtn.snp.makeConstraints { (make) in
            make.top.equalTo(userNameLab.snp.bottom).offset(7)
            make.right.equalTo(-15)
            make.left.equalTo(userNameLab)
            
        }
        
        
        var tmpLab : UILabel?
        let arr_title = ["关注","粉丝","收藏"]
        for i in 0..<arr_title.count {
            let titleLab = UILabel()
            titleLab.textAlignment = .center
            titleLab.tag = 101 + i
            titleLab.text = "0"
            mainView.addSubview(titleLab)
            titleLab.textColor = KYP_333333_COLOR
            titleLab.font = UIFont(name: PF_Medium, size: 15)
            titleLab.snp.makeConstraints { (make) in
                make.top.equalTo(descBtn.snp.bottom).offset(30)
                if tmpLab != nil{
                    make.left.equalTo(tmpLab!.snp.right).offset(50)
                }else{
                    make.left.equalTo(15)
                }
            }
            
            let descLab = UILabel()
            descLab.text = arr_title[i]
            mainView.addSubview(descLab)
            descLab.textAlignment = .center
            descLab.textColor = KYP_666666_COLOR
            descLab.font = UIFont(name: PF_Regular, size: 12)
            descLab.snp.makeConstraints { (make) in
                make.left.equalTo(titleLab.snp.right).offset(5)
                make.centerY.equalTo(titleLab)
            }
           
            tmpLab = descLab
        }
        
        let centerLineView = UIView()
        addSubview(centerLineView)
        centerLineView.backgroundColor = TABLE_LINE_COLOR
        centerLineView.snp.makeConstraints { (make) in
            make.left.equalTo(15)
            make.right.equalTo(-15)
            make.top.equalTo(tmpLab!.snp.bottom).offset(10)
            make.height.equalTo(0.5)
        }
        
        
        mainView.addSubview(bottomLab)
        bottomLab.text =  "我的文章（0）"
        bottomLab.textAlignment = .left
        bottomLab.textColor = KYP_333333_COLOR
        bottomLab.font = UIFont(name: PF_Medium, size: 15)
        bottomLab.snp.makeConstraints { (make) in
            make.left.equalTo(15)
            make.top.equalTo(centerLineView.snp.bottom).offset(0)
            make.bottom.equalTo(0)
            make.height.equalTo(55)

        }
        
        let redLineView = UIView()
        addSubview(redLineView)
        redLineView.backgroundColor = .red
        redLineView.snp.makeConstraints { (make) in
            make.left.equalTo(bottomLab)
            make.width.equalTo(60)
            make.top.equalTo(bottomLab.snp.bottom)
            make.height.equalTo(1)
        }
        
        
//        10035
        let btnHeight = 35
        editBtn.layer.borderColor = KYP_999999_COLOR.cgColor
        editBtn.layer.borderWidth = 0.5
        editBtn.layer.cornerRadius = CGFloat(btnHeight/2)
        editBtn.layer.masksToBounds = true
        editBtn.setTitleColor(KYP_333333_COLOR, for: .normal)
        editBtn.setTitle("编辑资料", for: .normal)
        editBtn.titleLabel?.font = UIFont.systemFont(ofSize: 15)
        mainView.addSubview(editBtn)
        editBtn.snp.makeConstraints { (make) in
            make.top.equalTo(10)
            make.right.equalTo(-15)
            make.height.equalTo(btnHeight)
            make.width.equalTo(100)
        }
        
    }
    //MARK:-获取用户资料
    func getUserInfo(userId : String){
        let url =  "api/services/app/UserProfile/GetUserProfileAsync?id=\(userId)"
        SDataCenter.shared.SGetJsonDataWithUrl(str_url: url, params: nil) { (responseData, isSuccess) in
            if isSuccess && responseData is [String : Any]{
                if let model = PersonUserModel.deserialize(from: responseData as? [String : Any]){
                    self.setUserInfo(model: model)
                }
                
                
            }else{
                
                
            }
        }
    }
    func setUserInfo(model:PersonUserModel) {
        userNameLab.text = model.nickName != nil ?  model.nickName : "未设置昵称"
        if let str_url = model.avatar?.url{
            userImg.kf.setImage(with: URL(string: str_url),placeholder:UIImage(named: "login_head_moren") )
        }
        descBtn.setTitle(model.headline != nil ?  model.headline : " 这家伙很懒，什么也没留下", for: .normal)
        
        setLabWith(tag: 101, title: model.followedCount)
        setLabWith(tag: 102, title: model.followerCount)
        setLabWith(tag: 103, title: model.favoriteCount)
//        setLabWith(tag: 104, title: model.viewCount)
        bottomLab.text =  "我的文章（0）"

     
    }
    func setLabWith(tag : Int , title : String?) -> Void{
        if let tmpLb = viewWithTag(tag) as? UILabel{
            
            tmpLb.text = title != nil ? title : "0"
        }
    }
    
}
